package com.mldong.modules.cms.controller;

import com.mldong.auth.Perm;
import com.mldong.base.CommonPage;
import com.mldong.base.CommonResult;
import com.mldong.base.IdParam;
import com.mldong.base.IdsParam;
import com.mldong.modules.cms.dto.ContentPageParam;
import com.mldong.modules.cms.dto.ContentParam;
import com.mldong.modules.cms.service.ContentService;
import com.mldong.modules.cms.vo.ContentVO;
import com.mldong.validation.Groups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
    * 内容 前端控制器
    * </p>
*
* @author mldong
* @since 2022-10-23
*/
@RestController
@Api(tags = "内容管理")
@RequiredArgsConstructor
public class ContentController {
    private final ContentService contentService;
    /**
     * 添加内容
     * @param param
     * @return
     */
    @PostMapping("/cms/content/save")
    @ApiOperation(value = "添加内容")
    @Perm
    public CommonResult<?> save(@RequestBody @Validated({Groups.Save.class}) ContentParam param) {
        contentService.save(param);
        return CommonResult.ok();
    }
    /**
     * 删除内容
     * @param param
     * @return
     */
    @PostMapping("/cms/content/remove")
    @ApiOperation(value = "删除内容")
    @Perm
    public CommonResult<?> remove(@RequestBody IdsParam param) {
        contentService.removeBatchByIds(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 修改内容
     * @param param
     * @return
     */
    @PostMapping("/cms/content/update")
    @ApiOperation(value = "修改内容")
    @Perm
    public CommonResult<?> update(@RequestBody @Validated({Groups.Update.class}) ContentParam param) {
        contentService.update(param);
        return CommonResult.ok();
    }
    /**
     * 查询单个内容
     * @param param
     * @return
     */
    @PostMapping("/cms/content/detail")
    @ApiOperation(value = "查询单个内容")
    @Perm
    public CommonResult<ContentVO> detail(@RequestBody IdParam param) {
        ContentVO content = contentService.findById(param.getId());
        return CommonResult.data(content);
    }
    /**
     *分页查询内容列表
     * @param param
     * @return
     */
    @PostMapping("/cms/content/page")
    @ApiOperation(value = "分页查询内容列表")
    @Perm
    public CommonResult<CommonPage<ContentVO>> page(@RequestBody ContentPageParam param) {
        return CommonResult.data(contentService.page(param));
    }
}
